﻿using NoteHelper.IBusiness;
using NoteHelper.Utils;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace NoteHelper
{
    public class ArticleListFormController
    {
        private int rowIndex;
        private IBaseView view;
        private IArticleManager articleManager;
        public ArticleListFormController(IBaseView baseView, IArticleManager manager)
        {
            view = baseView;
            articleManager = manager;

            view.Insert += Form_Insert;
            view.Edit += Form_Edit;
            view.Remove += Form_Remove;
            view.Copy += Form_Copy;
            view.Query += Form_Query;
            view.Accept += Form_Accept;
            view.Cancel += Form_Cancel;
            view.Print += Form_Print;
            view.Help += Form_Help;
            var ht = new Hashtable();
            ht.Add("TypeName", "db");
            Query(ht);
        }
        private void Form_Insert(object sender, EventArgs e)
        {
            view.RowData = null;
            view.Cmd = FormCommand.Insert;
            view.BeginEdit();
        }
        private void Form_Edit(object sender, EventArgs e)
        {
            BeginEdit(FormCommand.Edit);
        }
        private void Form_Remove(object sender, EventArgs e)
        {
            Form_Cancel(null, null);
            rowIndex = view.GetRowIndex();
            if (rowIndex == -1) return;
            
            var deleteRow = view.DisplayList.Rows[rowIndex];
            var id = int.Parse(deleteRow["ObjectID"].ToString());
            articleManager.Delete(id);
            view.DisplayList.Rows.RemoveAt(rowIndex);
            for (int i = rowIndex; i < view.DisplayList.Rows.Count; i++)
            {
                var row = view.DisplayList.Rows[i];
                row["$_rowIndex"] = i;
            }
        }
        private void Form_Copy(object sender, EventArgs e)
        {
            throw new NotImplementedException();
            //BeginEdit(FormCommand.Insert);
        }
        private void BeginEdit(FormCommand cmd)
        {
            rowIndex = view.GetRowIndex();
            if (rowIndex == -1) return;

            var selectedRow = view.DisplayList.Rows[rowIndex];
            var ht = view.RowData;
            ht.GetValueFormDataRow(selectedRow);
            view.RowData = ht;
            view.Cmd = cmd;
            view.BeginEdit();
        }
        private void Form_Query(object sender, EventArgs e)
        {
            Query(view.RowData);
        }
        private void Form_Accept(object sender, EventArgs e)
        {
            bool error = false;
            switch (view.Cmd)
            {
                case FormCommand.Display:
                    break;
                case FormCommand.Insert:
                    //实例化var insertEntity = new Model.ProjectForm();
                    // 填充数据
                    //view.RowData.FillValueToEntity(insertEntity);
                    // 更新数据库
                    var insertRow = view.DisplayList.NewRow();
                    // 填充数据到insertRow;
                    insertRow["$_rowIndex"] = view.DisplayList.Rows.Count;
                    view.DisplayList.Rows.Add(insertRow);
                    break;
                case FormCommand.Edit:
                    var editRow = view.DisplayList.Rows[rowIndex];
                    var id = editRow["ObjectID"];
                    break;
                case FormCommand.Query:
                    Query(view.RowData);
                    break;
                default:
                    break;
            }
            if (error) return;
            view.RowData = null;
            view.Cmd = FormCommand.Display;
            view.EndEdit();
        }
        private void Query(Hashtable ht)
        {
            // 查询数据库
            string key = "keyword";
            key = ht.ContainsKey(key) ? ht[key].ToString() : "db";
            string keystr = StringHelper.MergeSpace(key);            
            if (!string.IsNullOrEmpty(keystr))
            {
                ht.Add("TypeNameList", keystr.Split(' ').Select(t => "%" + t + "%").ToList());
            }
            var table = articleManager.QueryForDataTable("UserQueryArticle", ht);
            table.Columns.Add("$_rowIndex", typeof(int));
            for (int i = 0; i < table.Rows.Count; i++)
            {
                var row = table.Rows[i];
                row["$_rowIndex"] = i;
            }
            view.DisplayList = table;
        }

        private void Form_Cancel(object sender, EventArgs e)
        {
            view.RowData = null;
            view.Cmd = FormCommand.Display;
            view.EndEdit();
        }
        private void Form_Print(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
        private void Form_Help(object sender, EventArgs e)
        {
            var helpBuilder = new StringBuilder();
            helpBuilder.Append(":h 帮助\r\n");
            helpBuilder.Append(":n 添加\r\n");
            helpBuilder.Append(":e 修改\r\n");
            helpBuilder.Append(":d 删除\r\n");
            helpBuilder.Append(":l 显示所有\r\n");
            AlterMsg.Info(helpBuilder.ToString());
        }
    }
}
